"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const subscribe_1 = __importDefault(require("./subscribe"));
const unsubscribe_1 = __importDefault(require("./unsubscribe"));
const disconnect_1 = __importDefault(require("./disconnect"));
const message_1 = __importDefault(require("./message"));
const notice_1 = __importDefault(require("./notice"));
const middlewares_1 = require("./middlewares");
const webrtc_1 = __importDefault(require("../webrtc"));
const Member_1 = require("../lib/Member");
const enums_1 = require("../utils/enums");
const Rooms_1 = require("../lib/Rooms");
const classType = enums_1.RoomTypeEnums.RESIDENT;
const inviteMode = enums_1.InviteModeEnums.NO_VERIFY;
const joinMode = enums_1.JoinModeEnums.NO_VERIFY;
const chatMode = enums_1.ChatModeEnums.GROUP;
function runWebSocketServer(app) {
    const { io, rooms: lRoom } = app.locals;
    const intercom = io.of('/intercom');
    app.locals.intercom = intercom;
    intercom.on('connection', function (socket) {
        const query = socket.handshake.query;
        const { classId, classname, userId, username } = query;
        const roomOption = {
            classId, classname, classType, inviteMode, joinMode, chatMode
        };
        const owner = (socket.owner = { userId, username, socketId: socket.id });
        app.locals.socket = socket;
        function connection() {
            const __owner = Member_1.Member.createMember(owner);
            socket.join(classId);
            if (!lRoom[classId]) {
                lRoom[classId] = new Rooms_1.Room(roomOption);
            }
            lRoom[classId].setMember(__owner);
            intercom[userId] = socket;
            (0, middlewares_1.middlewares)(socket);
            (0, message_1.default)(app);
            (0, webrtc_1.default)(app);
            (0, notice_1.default)(app);
            (0, subscribe_1.default)(app);
            (0, unsubscribe_1.default)(app);
            (0, disconnect_1.default)(app);
        }
        if (app.locals.members[userId]) {
            const repeatOption = {
                status: 'error',
                message: 'Repeat login'
            };
            socket
                .timeout(1000 * 10)
                .emit('repeat', repeatOption, (error) => {
                if (error) {
                    socket.emit('error', {
                        status: 'error',
                        message: 'Repeat login'
                    });
                    socket.disconnect();
                }
                else {
                    app
                        .locals
                        .sockets[userId]
                        .timeout(300)
                        .emit('error', {
                        status: 'error',
                        message: 'Repeat login'
                    }, () => {
                        app.locals.sockets[userId].disconnect();
                        connection();
                    });
                }
            });
        }
        else {
            connection();
        }
        console.log('connection');
    });
    io.on('leave', function () {
        console.log('-----------');
        io.emit('disconnect');
    });
}
exports.default = runWebSocketServer;
